package com.longdaji.decoration.ui.comment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.longdaji.decoration.R;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.data.bean.Comment;
import com.longdaji.decoration.ui.creditassess.CreditAssessActivity;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by longtao on 2017/12/19.
 */

public class CriticismActivity extends BaseActivity implements CriticismContract.View, SwipeRefreshLayout.OnRefreshListener {

    private static final String TAG = CreditAssessActivity.class.getSimpleName();
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    @BindView(R.id.recycler_view_comments)
    RecyclerView mRecyclerViewComments;
    @BindView(R.id.tv_no_data)
    TextView mTvNoData;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout mSwipeRefresh;
    private CriticismAdapter mAdapter;
    @Inject
    CriticismPresenter mPresenter;
    private String mGoodId;


    public static Intent getStartIntent(Context context) {
        Intent intent = new Intent(context, CriticismActivity.class);
        return intent;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "CriticismActivity onCreate");
        setContentView(R.layout.activity_criticism);
        setUnBinder(ButterKnife.bind(this));
        initView();
        if (getIntent() != null) {
            mGoodId = getIntent().getStringExtra("good_id");
            initData(mGoodId);
        }

    }

    private void initView() {
        mPresenter.setView(this);
        mTvTitle.setText("全部评价");
        initReclerView();
        initSwipe();
    }

    private void initSwipe() {
        // 设置颜色属性的时候一定要注意是引用了资源文件还是直接设置16进制的颜色，因为都是int值容易搞混
        // 设置下拉进度的背景颜色，默认就是白色的
        mSwipeRefresh.setProgressBackgroundColorSchemeResource(android.R.color.white);
        // 设置下拉进度的主题颜色
        mSwipeRefresh.setColorSchemeResources(R.color.colorAccent, R.color.colorPrimary, R.color.colorPrimaryDark);
        // 下拉时触发SwipeRefreshLayout的下拉动画，动画完毕之后就会回调这个方法
        mSwipeRefresh.setOnRefreshListener(this);
    }

    @Override
    protected void onDestroy() {
        mPresenter.detachView();
        super.onDestroy();
    }

    private void initReclerView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerViewComments.setLayoutManager(layoutManager);
        mAdapter = new CriticismAdapter(this, new ArrayList<Comment>(), new CriticismAdapter.CriticismListener() {
            @Override
            public void onLikeClick(Comment comment, int position) {
                if ("0".equals(comment.getIsLike())) {
                    mPresenter.likeOrDislikeGoods(Account.getInstance().getUserid(), comment.getCommentsId(), "1", position);
                } else {
                    mPresenter.likeOrDislikeGoods(Account.getInstance().getUserid(), comment.getCommentsId(), "0", position);
                }
            }
        });
        mRecyclerViewComments.setAdapter(mAdapter);
    }

    private void initData(String goodId) {

        mSwipeRefresh.setRefreshing(true);
        mPresenter.getCommentList(goodId, Account.getInstance().getUserid());
    }

    @OnClick({R.id.iv_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void showCommentList(List<Comment> commentList) {
        mSwipeRefresh.setRefreshing(false);
        if (commentList == null || commentList.size() == 0) {
            showCommentListNone();
        } else {
            mTvNoData.setVisibility(View.GONE);
            mRecyclerViewComments.setVisibility(View.VISIBLE);
            mAdapter.setData(commentList);
        }

    }

    @Override
    public void showCommentListNone() {
        mSwipeRefresh.setRefreshing(false);
        mTvNoData.setVisibility(View.VISIBLE);
        mRecyclerViewComments.setVisibility(View.GONE);
    }

    @Override
    public void showLikeSuccess(int position) {
        Comment comment = mAdapter.getItemByPosition(position);
        comment.setIsLike("1");
        comment.setCommentLike(comment.getCommentLike() + 1);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showLikeFail() {
        toast("点赞失败！");
    }

    @Override
    public void showDisLikeSuccess(int position) {
        Comment comment = mAdapter.getItemByPosition(position);
        comment.setIsLike("0");
        comment.setCommentLike(comment.getCommentLike() - 1);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showDisLikeFail() {
        toast("取消点赞失败！");
    }

    @Override
    public void onRefresh() {
        initData(mGoodId);
    }
}
